package com.alibaba.system.controller;

import com.alibaba.basic.query.BaseQuery;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.alibaba.system.domain.DictionaryType;
import com.alibaba.system.service.DictionaryDetailService;
import com.alibaba.system.service.DictionaryTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/dict")
public class DictionaryTypeController {
    @Autowired
    private DictionaryTypeService dictionaryTypeService;
    @Autowired
    private DictionaryDetailService dictionaryDetailService;

    // 查询所有
    @GetMapping
    public List<DictionaryType> findAll() {
        return dictionaryTypeService.findAll();
    }
    // 单个查询
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public DictionaryType findById(@PathVariable("id")Long id) {
        return dictionaryTypeService.findById(id);
    }

    // 添加和修改,需要返回ajax结果
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if(dictionaryType.getId()==null){// 没有id就是新增，id自增，有id就是修改，id存在只需要修改别的内容
                dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult deleteDictionaryType(@PathVariable("id")Long id) {
        try {
            dictionaryTypeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    // 批量删除
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            dictionaryTypeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    // 高级查询
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody BaseQuery baseQuery) {
        return dictionaryTypeService.query(baseQuery);
    }
    // 根据id查询对应的值
    @GetMapping("/d/{id}")
    public List<DictionaryType> findByDetail(@PathVariable("id")Long id) {

        return dictionaryTypeService.findByDetail(id);
    }
}
